<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        ><a href="/admin" @click="resetDefaultActivePath"
          >首页</a
        ></el-breadcrumb-item
      >
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容卡片区域 -->
    <el-card>
      <el-form :model="searchData" class="demo-form-inline">
        <el-form-item label="" style="width: 100%">
          <el-input
            v-model="searchData.key"
            placeholder="请输入关键字"
          ></el-input>
        </el-form-item>
        <el-form-item label="" style="width: 100%">
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item label="" style="width: 100%">
          <el-button type="primary" @click="addRole">添加</el-button>
        </el-form-item>
      </el-form>
      <!-- 中间表格区域 -->
      <el-table :data="tableData" style="width: 100%" :border="true">
        <el-table-column label="">
          <template #default="{ $index }">
            {{
              (searchData.currentPage - 1) * searchData.pageSize + $index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch
              v-model="row.roleState"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="showEditRoleDialog"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="destroyRole"
          ></el-button>
          <el-button type="warning" icon="el-icon-setting"></el-button>
        </el-table-column>
      </el-table>
    </el-card>

    <!--底部分页区域-->
    <el-pagination
      :current-page="searchData.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchData.pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

    <!--添加角色对话框-->
    <el-dialog
      title="添加角色"
      :visible.sync="addRoleDialogVisible"
      width="30%"
      @close="close"
    >
      <el-form
        ref="form"
        :model="roleData"
        :rules="addRoleRules"
        label-width="0px"
      >
        <el-form-item label="" prop="roleName">
          <el-input
            v-model="roleData.roleName"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="roleDesc">
          <el-input
            v-model="roleData.roleDesc"
            prefix-icon="el-icon-info"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="createRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { getRoles, getAddRoles } from "@/api/index";
import { ElForm } from "element-ui/types/form";
import { Component, Ref, Vue } from "vue-property-decorator";
@Component({
  name: "Roles",
  components: {},
})
export default class Roles extends Vue {
  private resetDefaultActivePath() {
    sessionStorage.removeItem("activePath");
  }
  private addRoleDialogVisible = false;
  // 搜索的数据
  private searchData = {
    key: "", // 关键字
    currentPage: 1, // 当前页
    pageSize: 5, // 页容量
  };
  private totalCount = 0;
  // 新增和编辑的数据
  private roleData = {
    id: "",
    roleName: "",
    roleDesc: "",
    roleState: true,
  };
  private addRoleRules = {
    roleName: [
      { required: true, message: "请输入角色名称", trigger: "blur" },
      { min: 2, message: "长度至少2个字符", trigger: "blur" },
    ],
    roleDesc: [
      { required: true, message: "请输入角色描述", trigger: "blur" },
      { min: 1, message: "长度至少1个字符", trigger: "blur" },
    ],
  };
  private tableData = [
    // { roleName: "超级管理员", roleDesc: "管理者", roleState: true },
    // { roleName: "员工", roleDesc: "啥都干", roleState: false },
    // { roleName: "财务", roleDesc: "管钱", roleState: true },
    // { roleName: "人力", roleDesc: "管人", roleState: true },
  ];
  private created() {
    this.getRoles();
  }
  // 获取所有角色
  private async getRoles() {
    const result: any = await getRoles(this.searchData);
    console.log(result);
    this.tableData = result.data.data.roles;
    this.totalCount = result.data.data.count;
  }
  // 查询
  private search() {
    this.getRoles();
  }
  // 新增
  @Ref() readonly form!: ElForm;
  private addRole() {
    this.addRoleDialogVisible = true;
    // 让弹窗出来后在去重置 使用@$nextTick
    this.$nextTick(() => {
      // 重置表单
      this.form.resetFields();
    });
  }
  // 编辑
  private showEditRoleDialog() {
    this.addRoleDialogVisible = true;
  }
  // 新增角色或编辑角色的确定按钮
  private createRole() {
    this.form.validate((valid) => {
      if (valid) {
        if (!this.roleData.id) {
          console.log('id为空');
          // 没有id则是新增角色
          getAddRoles(this.roleData).then((result) => {
            console.log(result);
          });
        }else{
          console.log('编辑角色相关代码');
        }
      }
    });
  }
  // 删除
  private destroyRole() {}

  // 分页相关
  private handleSizeChange(size: any) {}
  private handleCurrentChange(current: any) {}

  // 关闭弹窗
  private close() {
    this.roleData = {
      id: "",
      roleName: "",
      roleDesc: "",
      roleState: true,
    };
    this.addRoleDialogVisible = false;
    // 重置
    this.form.resetFields();
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  padding-bottom: 20px;
}
.demo-form-inline {
  display: flex;
  justify-content: center;
}
.query_role {
  padding: 0 20px;
}
.el-pagination {
  padding-top: 20px;
}
.el-button {
  margin-left: 15px;
}
.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
